<?php

namespace App\Http\Controllers\Back;

use App\Domain\BooksInDoor\Service\BooksInDoorService;
use App\Domain\ErrBook\Service\ErrBookService;
use App\Domain\Lendhist\Model\Lendhist;
use App\Domain\Lendhist\Service\LendhistService;
use App\Domain\Lendlist\Service\LendlistService;
use App\Http\Service\ServiceLtBook;
use App\Model\Book;
use App\Util\Result;
use Illuminate\Http\Request;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class LtLogController extends Controller
{
    use ServiceLtBook;

    private function leftBar() {
        return [
            'title' => '图书管理',
            'list'  => [
                [
                    'title' => '书籍仓库',
                    'type' => 'book_list',
                    'url' => url('back/book'),
                ],
                [
                    'title' => '待上架书籍',
                    'type' => 'collect',
                    'url' => url('back/book/collect'),
                ],
                [
                    'title' => '在架书籍',
                    'type' => 'sjBook',
                    'url' => url('back/sjBook/index'),
                ],
                [
                    'title' => '外借图书',
                    'type' => 'ltBook',
                    'url' => url('back/ltBook/index'),
                ],
                [
                    'title' => '下架书籍',
                    'type' => 'xjBook',
                    'url' => url('back/xjBook/index'),
                ],
                [
                    'title' => '外售图书',
                    'type' => 'wsBook',
                    'url' => url('back/wsBook/index'),
                ],
                [
                    'title' => '异常书籍',
                    'type' => 'ycBook',
                    'url' => url('back/ycBook/index'),
                ],
                [
                    'title' => '流通日志',
                    'type' => 'ltlog',
                    'url' => url('back/ltlog/index'),
                ],
            ]
        ];
    }



    protected $lendhistService=null;
    protected $lendlistService=null;
    protected $booksInDoorService=null;


    /**
     * @param Request $request
     * czs
     */
    public function __construct(Request $request,LendlistService $lendlistService,LendhistService $lendhistService,BooksInDoorService $booksInDoorService){
        $this->request = $request;
        $this->limit = config('config.pageLimit');
        $leftBarArr = $this->leftBar();
        $this->lendhistService=$lendhistService;
        $this->lendlistService=$lendlistService;
        $this->booksInDoorService=$booksInDoorService;
        view()->share(['title' => $leftBarArr['title'],'list'=>$leftBarArr['list'],'limit'=>$this->limit]);
    }

    /**
     * 图书列表
     * czs
     */
    public function index() {
        $keyWord = $this->request->input('keyword');
        $res = self::getLtlogs($keyWord,$this->limit)->getData();
        $shelves=DB::table('doors as a')
            ->leftjoin('shelves as b','a.sid','=','b.id')
            ->where('b.type',1)
            ->select('b.name','a.code','a.column')
            ->get();
        $this->request->flash();
        return view('back/ltLog/index')
            ->with('res',$res['list'])
            ->with('count',$res['count'])
            ->with('shelves',$shelves)
            ->with('titleType','ltlog');
    }

    public function ghcol($id) {
        DB::beginTransaction();
        try {
            //添加关系
            $this->lendlistService->delListByBookid($id);
            //添加关系日志
            $this->lendhistService->updateLendHistByBookId($id,'0',Lendhist::STATUS_1);
            //更改图书状态
            DB::table('Items')
                ->where('SerialNo',$id)
                ->update(['LTStatus'=>2]);
            DB::commit();
        }catch (\Exception $e) {
            DB::rollback();
            return Result::failure()->setType(4)->getResult();
        }

        //回调APP接口
        $data['book_id'] = $id;
        $data['cabinet'] = '0';
        $url="https://api.lexiangyueshu.com/callback/return";
        $ci = curl_init();
        curl_setopt($ci, CURLOPT_TIMEOUT, 30);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
        $params = http_build_query($data);
        if (!empty($params)) {
            curl_setopt($ci, CURLOPT_POST, true);
            curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
        }
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ci, CURLOPT_URL, $url);
        curl_exec($ci);
        curl_close($ci);
        return Result::success()->getResult();
    }

    public function gh() {
        $id=$this->request->input('id');
        $code=$this->request->input('code');
        $bookUid=$this->request->input('uid');

        DB::beginTransaction();
        try {
            //添加关系
            $this->lendlistService->delListByBookid($id);
            //添加关系日志
            $this->lendhistService->updateLendHistByBookId($id,$code,Lendhist::STATUS_1);
            //添加门书关系
            $this->booksInDoorService->returnUpdateRelation($code,$bookUid);

            //更改图书状态
            DB::table('Items')
                ->where('SerialNo',$id)
                ->update(['LTStatus'=>3]);
            DB::commit();
        }catch (\Exception $e) {
            DB::rollback();
            return Result::failure()->setType(4)->getResult();
        }

        //回调APP接口
        $data['book_id'] = $id;
        $data['cabinet'] = '0';
        $url="https://api.lexiangyueshu.com/callback/return";
        $ci = curl_init();
        curl_setopt($ci, CURLOPT_TIMEOUT, 30);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
        $params = http_build_query($data);
        if (!empty($params)) {
            curl_setopt($ci, CURLOPT_POST, true);
            curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
        }
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ci, CURLOPT_URL, $url);
        curl_exec($ci);
        curl_close($ci);
        return Result::success()->getResult();
    }
}
